#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
# ----------------------------------------------------------------------------
#  Purpose:     用于实现hash相关功能

#  Author:      青衫少年春归也

#  Created:     2020.01.02

# ----------------------------------------------------------------------------
'''


import hashlib
from typing import Callable


class StreamHasher():
    def __init__(self, algorithm:str="Md5", chunk_size:int=4096):
        self.chunksize = chunk_size
        if algorithm and hasattr(hashlib, algorithm):
            self.algorithm = getattr(hashlib, algorithm)
        else:
            self.algorithm = hashlib.md5

    def file_computer(self, stream:Callable) -> str:
        """
        计算的文件流的hash值
        参数信息：
            :param stream:文件流对象
        返回值：
            :return: 计算得到的hash值
        """
        for chunk in iter(lambda: stream.read(self.chunksize), ""):
            self.algorithm.update(chunk.encode("utf-8"))
        return self.algorithm.hexigest()


from hypothesis import given
import hypothesis.strategies as st

@given(x=st.lists(st.integers()), y=st.integers())
def test_ints_cancel(x, y):
    print(x, y)
    assert y in x

test_ints_cancel()